package com.datatypes;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Bitmap;

public class FriendsZone implements Serializable {

	private List<Friend> friends = null;

	public FriendsZone() {
		friends = new ArrayList<Friend>();
	}

	public FriendsZone(List<String> friendsFbId, ActivityTracks activityList) {
		friends = new ArrayList<Friend>();
		for (String fbID : friendsFbId) {
			Friend f = new Friend(fbID, null); // TODO function to get the name
												// from fbID
			f.setInActivity(isInActivity(activityList, fbID));
			friends.add(f);
		}
	}

	public FriendsZone(List<String> SporTeamFriends,
			ArrayList<Activity> activities) {
		friends = new ArrayList<Friend>();

		String facebookID;
		String name;
		String InActivtyStr;
		boolean InActivity;
		for (String str : SporTeamFriends) {

			// retrieve friend information from string

			// retrieve faceook ID
			facebookID = GetInfoFromStr(str, 0);

			// retrieve friend name
			name = GetInfoFromStr(str, 1);

			// retrieve activity status
			InActivtyStr = GetInfoFromStr(str, 2);

			if (InActivtyStr.equals("in_act"))
				InActivity = true;
			else
				InActivity = false;

			// create new sporteam friend
			Friend f = new Friend(facebookID, name);

			f.setInActivity(InActivity);

			friends.add(f);
		}
	}

	public FriendsZone(List<String> SporTeamFriends) {

		friends = new ArrayList<Friend>();

		String facebookID;
		String name;
		String InActivtyStr;
		boolean InActivity;
		for (String str : SporTeamFriends) {

			// retrieve friend information from string

			// retrieve faceook ID
			facebookID = GetInfoFromStr(str, 0);

			// retrieve friend name
			name = GetInfoFromStr(str, 1);

			// retrieve activity status
			InActivtyStr = GetInfoFromStr(str, 2);

			if (InActivtyStr.equals("in_act"))
				InActivity = true;
			else
				InActivity = false;

			// create new sporteam friend
			Friend f = new Friend(facebookID, name);

			//update activity status
			f.setInActivity(InActivity);

			// add friend to friend zone
			friends.add(f);
		}
	}

	// for debug
	public FriendsZone(int i) {
		friends = new ArrayList<Friend>();
		Friend f = new Friend("853435462", "Micha Sherman");
		friends.add(f);
		f = new Friend("100003730751502", "Tester Jones");
		friends.add(f);
	}

	private String GetInfoFromStr(String str, int index) {
		StringBuilder ret_str = new StringBuilder();
		int counter = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == ';') {
				if (counter == index)
					break;
				else
					counter++;
			} else if (counter == index) {
				ret_str.append(str.charAt(i));
			}
		}

		return ret_str.toString();
	}

	private boolean isInActivity(ActivityTracks activityList, String fbID) {

		for (Activity a : activityList.getActivityList()) {
			if (a.getReceiverID().equals(fbID) || a.getSenderID().equals(fbID))
				return true;
		}
		return false;
	}

	private boolean isInActivity(ArrayList<Activity> activities, String fbID) {

		for (Activity a : activities) {
			if (a.getReceiverID().equals(fbID) || a.getSenderID().equals(fbID))
				return true;
		}
		return false;
	}

	public Friend getFriend(String id) {
		for (Friend f : friends)
			if (f.getFbID().equals(id))
				return f;
		return null;
	}

	public Friend getFriend(int position) {
		return this.friends.get(position);
	}

	public List<Friend> getFriends() {
		return friends;
	}

	public void setFriends(List<Friend> friends) {
		this.friends = friends;
	}

}
